<?php
/*	ADDED BY PRADEEP TO CREATE THUMBNAIL OF THE IMAGE	*/
class my_thumbnail{

	
	var $img;

	function my_thumbnail($imgfile, $destinationFile='')
	{
		
		//get GD information. see what types we can handle
		$gd_info = function_exists('gd_info') ? gd_info() : $this->_gd_info();
		
		preg_match("/\A[\D]*([\d+\.]*)[\D]*\Z/", $gd_info['GD Version'], $matches);
		list($this->_gd_version_string, $this->_gd_version_number) = $matches;
		$this->_gd_version = substr($this->_gd_version_number, 0, strpos($this->_gd_version_number, '.'));
		if ($this->_gd_version >= 2) {
			$this->_imagecreatefunction = 'imagecreatetruecolor';
			$this->_resize_function = 'imagecopyresampled';
		} else {
			$this->_imagecreatefunction = 'imagecreate';
			$this->_resize_function = 'imagecopyresized';
		}


		//detect image format
		//$this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile);
		$extensionArr = pathinfo($imgfile);	
		$this->img["format"] = $extensionArr['extension'];
		$this->img["format"]=strtoupper($this->img["format"]);

		//IF IMAGE IS CREATED BY THE TEMPORARY IMAGE FILE 
		if( TRIM($this->img["format"]) == ''){	
			$extensionArr = pathinfo($destinationFile);	
			$this->img["format"] = $extensionArr['extension'];
			$this->img["format"]=strtoupper($this->img["format"]);
		}

		if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
			//JPEG
			
			$this->img["format"]="JPEG";
			$this->img["src"] = ImageCreateFromJPEG ($imgfile);

			
		} elseif ($this->img["format"]=="PNG") {
			//PNG
			$this->img["format"]="PNG";
			$this->img["src"] = ImageCreateFromPNG ($imgfile);
		} elseif ($this->img["format"]=="GIF") {
			//GIF
			$this->img["format"]="GIF";
			$this->img["src"] = ImageCreateFromGIF ($imgfile);
		} elseif ($this->img["format"]=="WBMP") {
			//WBMP
			$this->img["format"]="WBMP";
			$this->img["src"] = ImageCreateFromWBMP ($imgfile);
		} else {
			//DEFAULT
			echo "Not Supported File";
			exit();
		}

		@$this->img["width"] = imagesx($this->img["src"]);	//origional width of the image
		@$this->img["height"] = imagesy($this->img["src"]);	//origional height of the image
		
		//default quality jpeg
		$this->img["quality"]=100;
	}

	function size_height($size=100)
	{
		//height
    	$this->img["height_thumb"]=$size;	//	thumb height of the image

    	@$this->img["width_thumb"] = ($this->img["height_thumb"]/$this->img["height"])*$this->img["width"];
	}

	function size_width($size=100)
	{
		//width
		$this->img["width_thumb"]=$size;	//thumb width of the image
    	@$this->img["height_thumb"] = ($this->img["width_thumb"]/$this->img["width"])*$this->img["height"];
	}

	function size_auto($size=100)
	{
		//size
		if ($this->img["width"]>=$this->img["height"]) {
    		$this->img["width_thumb"]=$size;
    		@$this->img["height_thumb"] = ($this->img["width_thumb"]/$this->img["width"])*$this->img["height"];
		} else {
	    	$this->img["height_thumb"]=$size;
    		@$this->img["width_thumb"] = ($this->img["height_thumb"]/$this->img["height"])*$this->img["width"];
 		}
	}

	//resize the width of the image as per the fixed height of the image
	function size_auto_width($size=100)
	{		
		$this->img["height_thumb"]=$size;
		@$this->img["width_thumb"] = ($this->img["height_thumb"]/$this->img["height"])*$this->img["width"];
	}
	
	//resize the HEIGHT of the image as per the fixed WIDTH of the image
	function size_auto_height($size=100)
	{		
		$this->img["width_thumb"]=$size;
		@$this->img["height_thumb"] = ($this->img["width_thumb"]/$this->img["width"])*$this->img["height"];
	}

	function jpeg_quality($quality)
	{
		//jpeg quality
		$this->img["quality"]=$quality;
	}

	function show()
	{
		//show thumb
		@Header("Content-Type: image/".$this->img["format"]);

		/* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor/ImageCreate function*/
		$functionname = $this->_imagecreatefunction;
		$this->img["des"] = $functionname($this->img["width_thumb"],$this->img["height_thumb"]);

		$resizeFunction = $this->_resize_function;
		$resizeFunction($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["width_thumb"], $this->img["height_thumb"], $this->img["width"], $this->img["height"]);

		if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
			//JPEG
			imageJPEG($this->img["des"],"",$this->img["quality"]);
		} elseif ($this->img["format"]=="PNG") {
			//PNG
			imagePNG($this->img["des"]);
		} elseif ($this->img["format"]=="GIF") {
			//GIF
			imageGIF($this->img["des"]);
//			echo "$path";

		} elseif ($this->img["format"]=="WBMP") {
			//WBMP
			imageWBMP($this->img["des"]);
		}
	}

	function save($save="")
	{			
				
		//save thumb
		if (empty($save)) 
			$save=strtolower("./thumb.".$this->img["format"]);

		/* change ImageCreateTrueColor/ImageCreate to ImageCreate if your GD not supported ImageCreateTrueColor function*/
		
		$functionname = $this->_imagecreatefunction;
		$this->img["des"] = $functionname($this->img["width_thumb"],$this->img["height_thumb"]);

		$resizeFunction = $this->_resize_function;
		$resizeFunction($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["width_thumb"], $this->img["height_thumb"], $this->img["width"], $this->img["height"]);

		if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
			//JPEG			
			imageJPEG($this->img["des"],"$save",$this->img["quality"]);
		} elseif ($this->img["format"]=="PNG") {
			//PNG
			imagePNG($this->img["des"],"$save");
		} elseif ($this->img["format"]=="GIF") {
			//GIF
			imageGIF($this->img["des"],"$save");
		} elseif ($this->img["format"]=="WBMP") {
			//WBMP
			imageWBMP($this->img["des"],"$save");
		}
	}

	/**
     * Reimplements the original PHP {@link gd_info()} function for older PHP versions
     * 
     * @access private
     * @return array associative array with info about the GD library of the server
     */ 
	function _gd_info() {
		$array = array(
        	"GD Version" => "",
			"FreeType Support" => false,
			"FreeType Linkage" => "",
			"T1Lib Support" => false,
			"GIF Read Support" => false,
			"GIF Create Support" => false,
			"JPG Support" => false,
			"PNG Support" => false,
			"WBMP Support" => false,
			"XBM Support" => false
		);
        
		$gif_support = 0;
		ob_start();
		eval("phpinfo();");
		$info = ob_get_contents();
		ob_end_clean();
      
		foreach(explode("\n", $info) as $line) {
			if(strpos($line, "GD Version") !== false)
				$array["GD Version"] = trim(str_replace("GD Version", "", strip_tags($line)));
			if(strpos($line, "FreeType Support") !== false)
				$array["FreeType Support"] = trim(str_replace("FreeType Support", "", strip_tags($line)));
			if(strpos($line, "FreeType Linkage") !== false)
				$array["FreeType Linkage"] = trim(str_replace("FreeType Linkage", "", strip_tags($line)));
			if(strpos($line, "T1Lib Support") !== false)
				$array["T1Lib Support"] = trim(str_replace("T1Lib Support", "", strip_tags($line)));
			if(strpos($line, "GIF Read Support") !== false)
				$array["GIF Read Support"] = trim(str_replace("GIF Read Support", "", strip_tags($line)));
			if(strpos($line, "GIF Create Support") !== false)
				$array["GIF Create Support"] = trim(str_replace("GIF Create Support", "", strip_tags($line)));
			if(strpos($line, "GIF Support") !== false)
				$gif_support = trim(str_replace("GIF Support", "", strip_tags($line)));
			if(strpos($line, "JPG Support") !== false)
				$array["JPG Support"] = trim(str_replace("JPG Support", "", strip_tags($line)));
			if(strpos($line, "PNG Support") !== false)
				$array["PNG Support"] = trim(str_replace("PNG Support", "", strip_tags($line)));
			if(strpos($line, "WBMP Support") !== false)
				$array["WBMP Support"] = trim(str_replace("WBMP Support", "", strip_tags($line)));
			if(strpos($line, "XBM Support") !== false)
				$array["XBM Support"] = trim(str_replace("XBM Support", "", strip_tags($line)));
		}
        
		if($gif_support === "enabled") {
			$array["GIF Read Support"] = true;
			$array["GIF Create Support"] = true;
		}

		if($array["FreeType Support"] === "enabled") {
			$array["FreeType Support"] = true;
		}
 
        if($array["T1Lib Support"] === "enabled") {
            $array["T1Lib Support"] = true;
        }
       
		if($array["GIF Read Support"] === "enabled") {
			$array["GIF Read Support"] = true;
		}
 
		if($array["GIF Create Support"] === "enabled") {
			$array["GIF Create Support"] = true;
		}

		if($array["JPG Support"] === "enabled") {
			$array["JPG Support"] = true;
		}
            
		if($array["PNG Support"] === "enabled") {
			$array["PNG Support"] = true;
		}
            
		if($array["WBMP Support"] === "enabled") {
			$array["WBMP Support"] = true;
		}
            
		if($array["XBM Support"] === "enabled") {
			$array["XBM Support"] = true;
		}

		return $array;
    }

}

?>